<?php
header("Content-type:text/html;charset=utf-8");
// 本类由系统自动生成，仅供测试用途
class wxLoginAction extends Action {

    /**
     *  获取微信授权
     */
    public function getWxLogin(){
        $this->getWxCode();
    }

    /**
     * 获取微信授权code
     */
    protected function getWxCode(){

        $appid = C('wx')['APPID'] ;

        $redirect_url = urlencode('http://wxppv5.duapp.com/index.php/wxLogin/getUserOpID');
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_userinfo&state=12#wechat_redirect";
        header("location:".$url);

    }

    /**
     * 获取微信accss_token
     */
    public function getUserOpID(){
        $appid = C('wx')['APPID'];
        $appserct = C("wx")['AppSecret'];
        $code = $_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appserct."&code=".$code."&grant_type=authorization_code";
        $res = GoCurl($url,'GET');
        $res = json_decode($res,true);
        if($res){
             $access_token = $res['access_token'];
             $openid = $res['openid'];
             // 获取用户详细信息
             $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
             $result = GoCurl($url,'GET');
             $result = json_decode($result,true);
             // 注册本网站
             $member = D('Member')->wxLoginRegister($result);
             if($member['status']==200){
                    session('uid',$member['data']);
                    $this->redirect("/");
             }else{
                    echo "微信登录遇到错误";
             }
        }
    }
}